package com.nfha.ikvm;

import java.io.File;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;

import com.nfha.ikvm.TaskAssign.Employee;
import com.nfha.ikvm.TaskAssign.GroupInfo;
import com.nfha.ikvm.TaskAssign.TaskInfo;



public class Main {

	
	
	public enum SkillsEnum{
		SKILL_A(1,"A"),
		SKILL_B(2,"卖淫"),
		SKILL_C(3,"B"),
		SKILL_D(4,"高利贷"),
		SKILL_E(5,"放火");
		
		private int num;
		
		private String value;
		
		private SkillsEnum(int num, String value) {
			this.num = num;
			this.value = value;
		}
		
		public static String getValueByNum(int num){
			for(SkillsEnum skill:SkillsEnum.values() ){
				if(skill.num==num){
					return skill.value;
				}
			}
			return null;
		}
		
		
		public static String[] random(int min){
			//随机产生技能个数
			int max=SkillsEnum.values().length;
			int[] skillTotalNum = randomArray(min, max,1);
			
			
			//具体的技能编号
			int[] skillDetail = randomArray(min, max, skillTotalNum[0]);
			ArrayList<String> list = new ArrayList<String>();
			for (int skillNum : skillDetail) {
				String skill = SkillsEnum.getValueByNum(skillNum);
				list.add(skill);
				
			}
			return (String[]) list.toArray(new String[list.size()]);
			
		}
		
		
	}
	public enum PortCodeEnum{
		PORT_CODE_A(1,"A"),
		PORT_CODE_B(2,"B"),
		PORT_CODE_C(3,"C"),
		PORT_CODE_D(4,"D"),
		PORT_CODE_E(5,"E"),
		PORT_CODE_F(6,"F"),
		PORT_CODE_G(7,"G");
		
		private int num;
		
		private String value;
		
		private PortCodeEnum(int num, String value) {
			this.num = num;
			this.value = value;
		}
		
		public static String getValueByNum(int num){
			for(PortCodeEnum skill:PortCodeEnum.values() ){
				if(skill.num==num){
					return skill.value;
				}
			}
			return null;
		}
		
		
		public static String random(int min){
			//随机生成码头年编号
			int[] portCodeNum = randomArray(min, PortCodeEnum.values().length,1);
			
			return PortCodeEnum.getValueByNum(portCodeNum[0]);
			
		}
		
	}
	
	
	
	protected static void randomTest() {

		//List<Employee> employees = loadEmployees();
		//Map<String,Employee> employessMap=loadEmployees();
		
		TaskAssign taskAssign=new TaskAssign();
		
		Map<String,String[]> data=new HashMap<>();
		
		data.put("1",new String[] { SkillsEnum.SKILL_A.value, SkillsEnum.SKILL_C.value});
		data.put("2",new String[] { SkillsEnum.SKILL_A.value, SkillsEnum.SKILL_B.value, SkillsEnum.SKILL_D.value});
		data.put("3",new String[] { SkillsEnum.SKILL_E.value});
		data.put("4",new String[] { SkillsEnum.SKILL_A.value, SkillsEnum.SKILL_C.value, SkillsEnum.SKILL_E.value, SkillsEnum.SKILL_E.value});
		data.put("5",new String[] { SkillsEnum.SKILL_B.value});
		data.put("6",new String[] { SkillsEnum.SKILL_D.value});
		data.put("7",new String[] { SkillsEnum.SKILL_C.value});
		data.put("8",new String[] { SkillsEnum.SKILL_B.value, SkillsEnum.SKILL_D.value, SkillsEnum.SKILL_E.value});
		
		
		
		
		taskAssign.loadEmployees(data);
		
		//设置全部人员
		taskAssign.setEmployeeOnDudy(false);
		
		//单独设置某些人员
		taskAssign.setEmployeeOnDudy("2",true);
		taskAssign.setEmployeeOnDudy("4",true);
		taskAssign.setEmployeeOnDudy("7",true);
		taskAssign.setEmployeeOnDudy("8",true);
		
		
		for (int i = 0; i < 100; i++) {
			String port = PortCodeEnum.random(1);
			String[] needSkills = SkillsEnum.random(1);
			
			//while(needSkills.length<2) {
			//	needSkills = SkillsEnum.random(1);
			//}
			
			System.out.println("\n\n"+i+" 申请任务   监管点:" + port + "  技能:" + toString(needSkills));
			
			
			
			
			//将选中的信息保存到集合中
			//historyGroupInfo.add(randGroupInfo);
			TaskInfo htaskInfo=new TaskInfo(i+"",port,needSkills);
			
			
			//2  增加X次（可设定，X>200）任务，任务所需要的技能为2到5个技能的随机，码头也随机，每次派单增加的任务都计入个人的待办。
			
			
			//List<GroupInfo> groupInfos = calGroupInfo(employessMap.values(), htaskInfo.portCode, htaskInfo.skills);
			
			List<GroupInfo> groupInfos=taskAssign.calGroupInfo(htaskInfo);
			//Map<String, GroupInfo> groupInfoMap = toMap(groupInfos);
			
			GroupInfo.print(groupInfos);

			htaskInfo.groupInfo = taskAssign.draw(groupInfos);
			System.out.println("抽中：" + htaskInfo.groupInfo);
			
			taskAssign.assignTask(htaskInfo);
			
			
			
		}
		
		
	}
	
	
	protected static void fileTest() throws Exception {
		
		String dir="C:\\Users\\Administrator.LAPTOP-GV96112I\\Documents\\WeChat Files\\hhland54133\\Files\\新建文件夹";
		
		for(File file:new File(dir).listFiles() ) {
			
			TaskAssign ta=TaskAssign.loadFromDat(file.getPath());
			System.out.println(file.getPath());
			System.out.println(ta.getAllSkillIds());
			System.out.println(ta.getAllProtCodes());
			Employee.print(ta.employeesMap.values());
			for(int i=0;i<5;i++) {
				System.out.println("派单:"+i+1);
				
			  TaskInfo taskInfo=new TaskInfo(i+"","A","B");
			  List<GroupInfo> groupInfos= ta.calGroupInfo(taskInfo);
			  GroupInfo.print(groupInfos);
			  GroupInfo group=GroupInfo.draw(groupInfos);
			  System.out.println("抽中:"+group.id);
			  taskInfo.groupInfo=group;
			  ta.assignTask(taskInfo);
			}
		}
	}
	

	public static void main(String[] args) throws Exception {

             fileTest();
	}
	
	
	
	
//	2.增加X次（可设定，X>200）任务，任务所需要的技能为2到5个技能的随机，码头也随机，每次派单增加的任务都计入个人的待办。
//	每次任务派完后，都显示派单情况和派单后每个人的待办、在办、已办、合计
	
	
	
	/**
	 * @param min	随机数所在范围 最小
	 * @param max	随机数所在范围 最大
	 * @param n:随机数的个数
	 * @return
	 */
	public static int[] randomArray(int min, int max, int n) {
		int len = max - min + 1;

		if (max < min || n > len) {
			return null;
		}

		// 初始化给定范围的待选数组
		int[] source = new int[len];
		for (int i = min; i < min + len; i++) {
			source[i - min] = i;
		}

		int[] result = new int[n];
		Random rd = new Random();
		for (int i = 0; i < result.length; i++) {
			int index = rd.nextInt(source.length);
			// 将随机到的数放入结果集
			result[i] = source[index];
			// 将待选数组中被随机到的数，用待选数组(len-1)下标对应的数替换
			source[index] = source[source.length-1];
			source=Arrays.copyOf(source, source.length-1);
		}
		return result;
	}

	public static String toString(Object...vals) {
		String re="";
		for(Object val:vals) {
			re+=val.toString()+",";
		}
		return re;
	}
	
}
